package org.example.leetcpde;

import net.bytebuddy.pool.TypePool;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.BitSet;
import java.util.jar.JarEntry;
import java.util.stream.Collectors;

public class removeDuplicates {

    @Test
    void run() {
        int[] nums = {0,1,2,2,3,3,4};
//        int[] nums = {1,1,2};
        int i = removeDuplicates(nums);
        System.out.println(i);
        System.out.println(Arrays.stream(nums).mapToObj(String::valueOf).collect(Collectors.joining()));
    }

    public int removeDuplicates(int[] nums) {
        int j = 1;
        for (int i = 0; i < nums.length; i++) {
            int v1 = nums[i], v2 = nums[j - 1];
            if (v1 != v2) {
                nums[j] = v1;
                j++;
            }
        }

        return j;

    }

    @Test
    void removeElement() {
        int[] nums = {0,1,2,2,3,0,4,2};
        int i = removeElement(nums, 2);
        System.out.println(i);
        System.out.println(Arrays.stream(nums).mapToObj(String::valueOf).collect(Collectors.joining()));
    }

    public int removeElement(int[] nums, int val) {
        int i = 0, j = 0;
        for (; i < nums.length; i++) {
            int v = nums[i];
            if (v != val) {
                nums[j++] = v;
            }
        }

        return j;
    }

    public int strStr(String haystack, String needle) {
        return haystack.indexOf(needle);
    }
}
